package biznussoft.metercor;

import java.util.ArrayList;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.MenuItemCompat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import biznussoft.metercor.dialogfragments.DatePickerDialogFragment;
import biznussoft.metercor.dialogfragments.SignaturetDialogFragmen;
import biznussoft.metercor.dialogfragments.TimePickerDialogFragment;
import biznussoft.metercor.fragments.ActivityDetailFragment;
import biznussoft.metercor.fragments.ExpensesFragment;
import biznussoft.metercor.fragments.ExpensesMasterFragment;
import biznussoft.metercor.fragments.GasMeterDetailsFragment;
import biznussoft.metercor.fragments.GasMeterManagement;
import biznussoft.metercor.fragments.MaterialFragment;
import biznussoft.metercor.fragments.MaterialMasterFragment;
import biznussoft.metercor.fragments.NewWaterMeterDataManagement;
import biznussoft.metercor.fragments.ServiceOrderDetailsFragment;
import biznussoft.metercor.fragments.ServiceOrderFragment;
import biznussoft.metercor.fragments.SignaturetDialogFragmen2;
import biznussoft.metercor.fragments.TimeFragment;
import biznussoft.metercor.fragments.TimeMasterFragment;
import biznussoft.metercor.fragments.WatermeterDetailsFragment;
import biznussoft.metercor.listeners.ABCListener;
import biznussoft.metercor.listeners.ActivityDetailListener;
import biznussoft.metercor.listeners.DialogListener;
import biznussoft.metercor.listeners.GlobalListener;
import biznussoft.metercor.listeners.InventoryListener;
import biznussoft.metercor.model.MapModel;
import biznussoft.metercor.utils.Session;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.salesforce.androidsdk.app.SalesforceSDKManager;


public class ServiceOrderActivity extends BaseActivity  implements DialogListener, ABCListener,InventoryListener,GlobalListener,ActivityDetailListener{

	ArrayList<MapModel> list = new ArrayList<MapModel>();
	private SupportMapFragment merchantLocationFragment;

	private GoogleMap googleMap;
	//	private ArrayList<LatLng> markerPoints;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_service_order);
	}



	/*@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		  MenuItem actionItem = menu.add("Logout");                 
	        MenuItemCompat.setShowAsAction(actionItem,MenuItemCompat.SHOW_AS_ACTION_ALWAYS | MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT);
	        actionItem.setIcon(R.drawable.logout); 

		return super.onCreateOptionsMenu(menu);
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		 Log.i("NCS","item.getItemId()  :"+item.getTitle()+": "+item.getOrder());
		 if(item.getTitle().equals("Logout")){
			 SalesforceSDKManager.getInstance().logout(this);}
		 if(item.getTitle().equals("MyTrack")){

				merchantLocationFragment = new SupportMapFragment();
				merchantLocationFragment.setArguments(getIntent().getExtras());
				FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
				transaction.replace(R.id.serviceOrderviewer, merchantLocationFragment);
				transaction.commit();
				final Handler	handler=new Handler();
				final Runnable r = new Runnable()
				{
					public void run() 
					{
						merchantMap();
					}
				};
				handler.postDelayed(r, 1000);

	 }
		return super.onOptionsItemSelected(item);
	}*/

	@Override
	public void onWarehouseClicked(final int position,final String category) {
		// TODO Auto-generated method stub
		if(position >= 0){                
			ServiceOrderDetailsFragment newFragment = new ServiceOrderDetailsFragment();
			FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.serviceOrderviewer, newFragment);
			transaction.addToBackStack(null);
			transaction.commit();
			new Handler().postDelayed(new Runnable() {                      
				@Override
				public void run() {
					ServiceOrderDetailsFragment warehouseFragment = (ServiceOrderDetailsFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
					warehouseFragment.getWarehouseDetails(position,category);
				}
			}, 1000);
		}
	}

	@Override
	public void onCategoryClicked(final int position) {

		if(position ==1){
            
		WatermeterDetailsFragment newFragment = new WatermeterDetailsFragment();
		FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
		transaction.replace(R.id.serviceOrderviewer, newFragment);
		transaction.addToBackStack(null);
		transaction.commit();
		new Handler().postDelayed(new Runnable() {                      
			@Override
			public void run() {
				WatermeterDetailsFragment warehouseFragment = (WatermeterDetailsFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
				//warehouseFragment.getWarehouseDetails();
			}
		}, 1000);
	
		}else{
            
		GasMeterDetailsFragment newFragment = new GasMeterDetailsFragment();
		FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
		transaction.replace(R.id.serviceOrderviewer, newFragment);
		transaction.addToBackStack(null);
		transaction.commit();
		new Handler().postDelayed(new Runnable() {                      
			@Override
			public void run() {
				GasMeterDetailsFragment warehouseFragment = (GasMeterDetailsFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
				//warehouseFragment.getWarehouseDetails());
			}
		}, 1000);
	
			
		}

	}

	@Override
	public void onNewContact(final String[] args) {

		/*ActivityDetailFragment newFragment = new ActivityDetailFragment();
		FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
		transaction.replace(R.id.serviceOrderviewer, newFragment);
		transaction.addToBackStack(null);
		transaction.commit();
		new Handler().postDelayed(new Runnable() {                      
			@Override
			public void run() {*/
		ActivityDetailFragment warehouseFragment = (ActivityDetailFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
		warehouseFragment.getSignature(args[0]);
		/*}
		}, 1000);*/
	}

	@Override
	public void onGetContacts(final int position, final String category) {



		if(position >= 0){                
			ActivityDetailFragment newFragment = new ActivityDetailFragment();
			FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.serviceOrderviewer, newFragment);
			transaction.addToBackStack(null);
			transaction.commit();
			new Handler().postDelayed(new Runnable() {                      
				@Override
				public void run() {
					ActivityDetailFragment warehouseFragment = (ActivityDetailFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
					warehouseFragment.getWarehouseDetails(position,category);
				}
			}, 1000);
		}


	}

	@Override
	public void getCategory(String category,final String id) {


		if(category.equalsIgnoreCase("Track")){

			merchantLocationFragment = new SupportMapFragment();
			merchantLocationFragment.setArguments(getIntent().getExtras());
			FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.serviceOrderviewer, merchantLocationFragment);
			transaction.commit();
			final Handler	handler=new Handler();
			final Runnable r = new Runnable()
			{
				public void run() 
				{
					merchantMap();
				}
			};
			handler.postDelayed(r, 1000);

		}
		if(category.equalsIgnoreCase("water")){

			
			NewWaterMeterDataManagement newFragment = new NewWaterMeterDataManagement();
			FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.serviceOrderviewer, newFragment);
			transaction.addToBackStack(null);
			transaction.commit();
		}
		if(category.equalsIgnoreCase("gas")){

			GasMeterManagement newFragment = new GasMeterManagement();
			FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.serviceOrderviewer, newFragment);
			transaction.addToBackStack(null);
			transaction.commit();

		}


		if (category.equalsIgnoreCase("time")) {

			TimeMasterFragment newFragment = new TimeMasterFragment();
			FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.serviceOrderviewer, newFragment);
			transaction.addToBackStack(null);
			transaction.commit();
			new Handler().postDelayed(new Runnable() {                      
				@Override
				public void run() {
					TimeMasterFragment warehouseFragment = (TimeMasterFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
					warehouseFragment.getTime(id);
				}
			}, 1000);

		}
		if (category.equalsIgnoreCase("material")) {
			MaterialMasterFragment newFragment = new MaterialMasterFragment();
			FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.serviceOrderviewer, newFragment);
			transaction.addToBackStack(null);
			transaction.commit();
			new Handler().postDelayed(new Runnable() {                      
				@Override
				public void run() {
					MaterialMasterFragment warehouseFragment = (MaterialMasterFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
					warehouseFragment.getMaterial(id);
				}
			}, 1000);
		}
		if (category.equalsIgnoreCase("expenses")) {
			ExpensesMasterFragment newFragment = new ExpensesMasterFragment();
			FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.serviceOrderviewer, newFragment);
			transaction.addToBackStack(null);
			transaction.commit();
			new Handler().postDelayed(new Runnable() {                      
				@Override
				public void run() {
					ExpensesMasterFragment warehouseFragment = (ExpensesMasterFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
					warehouseFragment.getExpenses(id);
				}
			}, 1000);
		}
	}

	@Override
	public void nextDialog(String type) {
		Log.i("NCS","Type is :"+type);
		if(type.equalsIgnoreCase("date")){
			DatePickerDialogFragment.newInstace().show(getSupportFragmentManager(), "dialog");}
		if(type.equalsIgnoreCase("time")){
			TimePickerDialogFragment.newInstace().show(getSupportFragmentManager(), "dialog");}
		if(type.equalsIgnoreCase("sign")){
			SignaturetDialogFragmen.newInstace().show(getSupportFragmentManager(), "dialog");
		}
		if(type.equalsIgnoreCase("sign2")){
			SignaturetDialogFragmen2.newInstace().show(getSupportFragmentManager(), "dialog");
		}
	}

	@Override
	public void getSignature(String base64) {
		// TODO Auto-generated method stub
		ActivityDetailFragment warehouseFragment = (ActivityDetailFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
		warehouseFragment.getSignature(base64);
	}



	@Override
	public void setDateTime(StringBuffer sb) {

		Log.i("NCS","Date is  :"+sb.toString());

		if(Session.sStartTime && Session.sActivity){
			ActivityDetailFragment warehouseFragment = (ActivityDetailFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
			warehouseFragment.setStartDate(sb.toString());
		}
		if(Session.sEndTime && Session.sActivity){
			ActivityDetailFragment warehouseFragment = (ActivityDetailFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
			warehouseFragment.setEndDate(sb.toString());
		}
		if(Session.sStartTime && Session.sTime){
			TimeFragment timeFragment = (TimeFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
			timeFragment.setStartDate(sb.toString());
		}
		if(Session.sEndTime && Session.sTime){
			TimeFragment timeFragment = (TimeFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
			timeFragment.setEndDate(sb.toString());
		}
		if(Session.sStartTime && Session.sExpense){
			ExpensesFragment expenseFragment = (ExpensesFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
			expenseFragment.setTransactionDate(sb.toString());
		}
		if(Session.sStartTime && Session.sNewTime){
			TimeMasterFragment expenseFragment = (TimeMasterFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
			expenseFragment.setStartDate(sb.toString());
		}
		if(Session.sEndTime && Session.sNewTime){
			TimeMasterFragment expenseFragment = (TimeMasterFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
			expenseFragment.setEndDate(sb.toString());
		}
	}



	@Override
	public void displayFields(final int position, String category) {
		// TODO Auto-generated method stub
		if(category.equalsIgnoreCase("TimeMaster")){
			TimeFragment newFragment = new TimeFragment();
			FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.serviceOrderviewer, newFragment);
			transaction.addToBackStack(null);
			transaction.commit();
			new Handler().postDelayed(new Runnable() {                      
				@Override
				public void run() {
					TimeFragment warehouseFragment = (TimeFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
					warehouseFragment.showRecords(position);
				}
			}, 1000);
		}
		if(category.equalsIgnoreCase("ExpenseMaster")){
			ExpensesFragment newFragment = new ExpensesFragment();
			FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.serviceOrderviewer, newFragment);
			transaction.addToBackStack(null);
			transaction.commit();
			new Handler().postDelayed(new Runnable() {                      
				@Override
				public void run() {
					ExpensesFragment warehouseFragment = (ExpensesFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
					warehouseFragment.showRecords(position);
				}
			}, 1000);
		}
		if(category.equalsIgnoreCase("MaterialMaster")){
			MaterialFragment newFragment = new MaterialFragment();
			FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.serviceOrderviewer, newFragment);
			transaction.addToBackStack(null);
			transaction.commit();
			new Handler().postDelayed(new Runnable() {                      
				@Override
				public void run() {
					MaterialFragment warehouseFragment = (MaterialFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
					warehouseFragment.showRecords(position);
				}
			}, 1000);
		}
	}



	@Override
	public void onServiceClicked(int position) {
		// TODO Auto-generated method stub

	}

	private void merchantMap(){

		googleMap = merchantLocationFragment.getMap();

		Log.i("RGT","Size is  :"+ServiceOrderFragment.mapList.size());

		/*MapModel madhapur = new MapModel();
		madhapur.setLatitude(17.4406);
		madhapur.setLongitude(78.3911);
		list.add(madhapur);
		MapModel begumpet = new MapModel();
		begumpet.setLatitude(17.4449);
		begumpet.setLongitude(78.4694);
		list.add(begumpet);
		MapModel hytech = new MapModel();
		hytech.setLatitude(17.4416);
		hytech.setLongitude(78.3826);
		list.add(hytech);
		MapModel kondapur = new MapModel();
		kondapur.setLatitude(17.46099);
		kondapur.setLongitude(78.35320);
		list.add(kondapur);
		MapModel Kbr = new MapModel();
		Kbr.setLatitude(17.42063);               
		Kbr.setLongitude(78.42161);
		list.add(Kbr);
		MapModel pangagutta = new MapModel();
		pangagutta.setLatitude(17.42938);
		pangagutta.setLongitude(78.45078);
		list.add(pangagutta);*/




		Log.i("ABC","Session.sLatitude : "+Session.sLatitude );

		for(MapModel i : ServiceOrderFragment.mapList)
		{


			// Setting a click event handler for the map

			LatLng latLng = new LatLng(i.getLatitude(), i.getLongitude());
			// Creating a marker
			MarkerOptions markerOptions = new MarkerOptions();

			// Setting the position for the marker
			markerOptions.position(latLng);

			// Setting the title for the marker.
			// This will be displayed on taping the marker
			markerOptions.title(i.getAddress());

			// Clears the previously touched position

			// Animating to the touched position         
			googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

			// Placing a marker on the touched position                           
			googleMap.addMarker(markerOptions).setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));


		}   

		if(Session.sLatitude >0 ){

			LatLng latLng = new LatLng(Session.sLatitude, Session.sLongitude);
			// Creating a marker
			MarkerOptions markerOptions = new MarkerOptions();

			// Setting the position for the marker
			markerOptions.position(latLng);

			// Setting the title for the marker.
			// This will be displayed on taping the marker
			markerOptions.title(Session.sAddress);

			// Clears the previously touched position

			// Animating to the touched position         
			googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
			googleMap.animateCamera(CameraUpdateFactory.zoomTo(10));                          

			// Placing a marker on the touched position                           
			googleMap.addMarker(markerOptions).setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
		}


	}



	@Override
	public void getSignature2(String base64) {
		// TODO Auto-generated method stub
		ServiceOrderDetailsFragment warehouseFragment = (ServiceOrderDetailsFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
		warehouseFragment.getSignature(base64);
	}



}
